package offer.tree;

import java.util.ArrayList;
import java.util.LinkedList;

/**
 * @author DengYuan2
 * @create 2021-02-27 19:26
 */
public class n_32_2 {
    public static void main(String[] args) {
        n_32_2 n = new n_32_2();
        TreeNode t1 = new TreeNode(8);
        TreeNode t2 = new TreeNode(6);
        TreeNode t3 = new TreeNode(10);
        TreeNode t4 = new TreeNode(5);
        TreeNode t5 = new TreeNode(7);
        TreeNode t6 = new TreeNode(9);
        TreeNode t7 = new TreeNode(11);
        t1.left = t2;
        t1.right = t3;
        t2.left = t4;
        t2.right = t5;
        t3.left = t6;
        t3.right = t7;
        ArrayList<ArrayList<Integer>> res = n.Print(t1);
        System.out.println(res);
    }

    ArrayList<ArrayList<Integer>> Print(TreeNode pRoot) {
        ArrayList<ArrayList<Integer>> list = new ArrayList<>();
        if (pRoot==null){
            return list;
        }
        LinkedList<TreeNode> queue = new LinkedList<>();
        queue.add(pRoot);
        while (!queue.isEmpty()){
            ArrayList<Integer> arrayList = new ArrayList<>();
            int count=queue.size();
            for (int i = 0; i < count; i++) {
                TreeNode parent = queue.poll();
                if (parent.left!=null){
                    queue.add(parent.left);
                }
                if (parent.right!=null){
                    queue.add(parent.right);
                }
                arrayList.add(parent.val);
            }
            list.add(arrayList);
        }
        return list;
    }
}
